package org.emet.service;

import java.util.Date;
import java.util.List;

import org.emet.Constants;
import org.emet.dao.CremationHistDao;
import org.emet.dao.CrematorDao;
import org.emet.entity.biz.CremationHist;
import org.emet.entity.biz.Cremator;
import org.emet.jee.BusinessException;
import org.emet.jee.core.dao.Page;
import org.emet.service.dto.CrematorCriteria;
import org.emet.service.dto.CrematorStatus;
import org.hibernate.ObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RemotingDestination
public class CrematorService {
	@Autowired
	private CremationHistDao cremationHistDao;
	@Autowired
	private CrematorDao crematorDao;
	private static Logger logger = LoggerFactory.getLogger(CrematorService.class);

	public void save(Cremator entity) {
		try {
			String oldStatus = crematorDao.getStatus(entity.getId());
			if (entity.getId() != null && oldStatus.equals(Constants.STATUS_BUSY + "")) {
				endAllBusy(entity.getId());
			}
			crematorDao.save(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void endAllBusy(Long id) {
		List<CremationHist> result = cremationHistDao.getAllBusy(id);
		for (CremationHist cremationHist : result) {
			cremationHist.setEndDate(new Date());
		}
	}

	public List<Cremator> getAllValid() {
		try {
			return crematorDao.findBy("status", "O");
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public List<Cremator> getAll() {
		try {
			return crematorDao.getAll();
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public Cremator get(Long id) {
		try {
			return crematorDao.get(id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void del(Long... ids) {
		for (Long id : ids) {
			if (notExist(id)) {
				continue;
			}
			try {
				crematorDao.delete(id);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	private boolean notExist(Long id) {
		return get(id) == null;
	}

	public Page<Cremator> search(Page<Cremator> page, CrematorCriteria criteria) {
		try {
			return crematorDao.search(page, criteria);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public List<CrematorStatus> getCurrStatus() {
		try {
			return cremationHistDao.getCurrCrematorStatus();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}
}
